/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fatclientnbp;

import AirLine.Airline;
import AirLine.Airport;
import Database.Airlines;
import Database.Airports;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeSet;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author smulle
 */
public class SearchPanel extends JPanel implements ActionListener{
     
    private static String SEARCH_ACTION = "SEARCH_ACTION";

    Airports airports;
    Airlines airlines;
    // Variables declaration - do not modify
    private javax.swing.JCheckBox airlineChackBoxes[];
    private javax.swing.JComboBox departureComboBox;
    private javax.swing.JComboBox arrivalComboBox;
    private javax.swing.JComboBox yearComboBox;
    private javax.swing.JComboBox monthComboBox;
    private javax.swing.JComboBox dayComboBox;
    private javax.swing.JComboBox passangersComboBox;
    private javax.swing.JComboBox sortingComboBox;
    private javax.swing.JLabel departureLabel = new JLabel("Departure Airport");
    private javax.swing.JLabel arrivalLabel = new JLabel("Destination Airport");
    private javax.swing.JLabel dateLabel = new JLabel("Departure date");
    private javax.swing.JLabel airlineLabel = new JLabel("Airlines to use");
    private javax.swing.JLabel yearLabel = new JLabel("Year");
    private javax.swing.JLabel monthLabel = new JLabel("Month");
    private javax.swing.JLabel dayLabel = new JLabel("Day");
    private javax.swing.JLabel passangersLabel = new JLabel("Numbers of passangers");
    private javax.swing.JLabel sortingLabel = new JLabel("Sorting:");
    private javax.swing.JButton searchButton = new JButton("Search Trip");
    private MainController controller;
    private MainModel model;
    private TreeSet<Airline> airlineSet = null;

    /** Creates new form MainFrame */
    public SearchPanel(MainController controller,MainModel model) {
        this.model = model;
        this.controller = controller;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        airports = new Airports();
        airlines = new Airlines();

        searchButton.setActionCommand(SEARCH_ACTION);

        searchButton.addActionListener(this);

        airlineSet = new TreeSet<Airline>();

        try {
            departureComboBox = new JComboBox(airports.getAirportsList().toArray());
            arrivalComboBox = new JComboBox(airports.getAirportsList().toArray());
            airlineSet = airlines.getAirlines();

            airlineChackBoxes = new JCheckBox[airlineSet.size()];

            Iterator<Airline> it = airlineSet.iterator();
            int counter = 0;
            while (it.hasNext()) {
                Airline airline = it.next();
                airlineChackBoxes[counter] = new JCheckBox(airline.getName());
                airlineChackBoxes[counter].setSelected(true);
                //airlineChackBoxes[counter].setText("hoho");
                counter++;
            }

        } catch (SQLException e) {
            System.out.println("SqlException Caught, probably because of connection to the database cannot me established");
            System.exit(0);
        }

        sortingComboBox = new JComboBox();

        sortingComboBox.addItem(new SortingContainer(SortingContainer.SORT_BY_PRICE));

        sortingComboBox.addItem(new SortingContainer(SortingContainer.SORT_BY_TIME));

        yearComboBox = new JComboBox();

        for (int i = 2008; i < 2050; i++) {
            yearComboBox.addItem(i);
        }

        monthComboBox = new JComboBox();

        for (int i = 1; i < 13; i++) {
            Month month = new Month(i);
            monthComboBox.addItem(month);
        }

        dayComboBox = new JComboBox();

        for (int i = 1; i < 32; i++) {
            dayComboBox.addItem(i);
        }

        passangersComboBox = new JComboBox();

        for (int i = 1; i < 10; i++) {
            passangersComboBox.addItem(i);
        }





        GridBagConstraints constraints = new GridBagConstraints();

        constraints.fill = GridBagConstraints.HORIZONTAL;

        constraints.weightx = 1;

        constraints.weighty = 1;

        constraints.gridx = 0;

        constraints.gridy = 0;

        this.setLayout(new GridBagLayout());

        this.add(arrivalLabel, constraints);

        constraints.gridy++;

        this.add(departureLabel, constraints);

        constraints.gridy++;

        this.add(passangersLabel, constraints);

        constraints.gridy++;

        this.add(dateLabel, constraints);

        //Jump 2 to get room for labels over comboboxes in x > 0
        constraints.gridy += 2;

        this.add(sortingLabel, constraints);

        constraints.gridy++;

        this.add(airlineLabel, constraints);

        for (int i = 0; i < airlineSet.size(); i++) {
            constraints.gridy++;
            this.add(airlineChackBoxes[i], constraints);
        }



        /// X = 1
        constraints.gridy = 0;

        constraints.gridx = 1;

        this.add(arrivalComboBox, constraints);

        constraints.gridy++;

        this.add(departureComboBox, constraints);

        constraints.gridy++;

        constraints.weightx = 0.5;

        this.add(passangersComboBox, constraints);

        constraints.gridy++;

        this.add(yearLabel, constraints);

        constraints.gridx++;

        this.add(monthLabel, constraints);

        constraints.gridx++;

        this.add(dayLabel, constraints);

        constraints.gridx = 1;

        constraints.gridy++;

        this.add(yearComboBox, constraints);

        constraints.gridx++;

        this.add(monthComboBox, constraints);

        constraints.gridx++;

        this.add(dayComboBox, constraints);

        constraints.gridx = 1;
        constraints.gridy++;

        this.add(sortingComboBox, constraints);

        constraints.gridx++;
        constraints.gridy += 5;

        this.add(searchButton, constraints);


    }

    public SearchParameters getSearchParameters() {
        int year = Integer.valueOf(yearComboBox.getSelectedItem().toString());
        int month = ((Month)monthComboBox.getSelectedItem()).getMonth();
        int day = Integer.valueOf(dayComboBox.getSelectedItem().toString());

        Timestamp earliestDeparture = new Timestamp(((year - 1970) * 31556926 + (month * 2629743) + day * 86400) * 1000);
        ArrayList<String> airlineList = new ArrayList<String>();

        for (int i = 0; i < airlineChackBoxes.length; i++) {
            if (airlineChackBoxes[i].isSelected()) {
                
               for (Airline a : airlineSet) {
                  if (   a.getName().equalsIgnoreCase(airlineChackBoxes[i].getText())) {
                    airlineList.add(a.getCode());
                  }
               }              
               
            }
        }

        return new SearchParameters((Airport) departureComboBox.getSelectedItem(), (Airport) arrivalComboBox.getSelectedItem(), Integer.valueOf(passangersComboBox.getSelectedItem().toString()), ((SortingContainer) sortingComboBox.getSelectedItem()).getSortType(), earliestDeparture, airports, airlineList);

    }

    public void actionPerformed(ActionEvent e) {
        
        
                SearchParameters searchParameters = getSearchParameters();
                controller.search(searchParameters);
          
            
        
    }
}
